package com.tantou.java.multi;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class FutureTaskTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> ft2 = new FutureTask<>(new T2Task());
        FutureTask<String> ft1 = new FutureTask<>(new T1Task(ft2));

        Thread t1 = new Thread(ft1);
        Thread t2 = new Thread(ft2);

        t1.start();
        t2.start();

        System.out.println(ft1.get());
    }
}

class T1Task implements Callable {

    private FutureTask<String> ft2;

    public T1Task(FutureTask ft2) {
        this.ft2 = ft2;
    }

    @Override
    public Object call() throws Exception {
        System.out.println("T1:洗水壶");
        TimeUnit.SECONDS.sleep(1);

        System.out.println("T1:烧开水");
        TimeUnit.SECONDS.sleep(1);

        String tf = ft2.get();
        return "上茶:"+tf;
    }
}

class T2Task implements Callable {

    @Override
    public Object call() throws Exception {
        System.out.println("T2:洗茶壶");
        TimeUnit.SECONDS.sleep(1);

        System.out.println("T2:洗茶杯");
        TimeUnit.SECONDS.sleep(1);

        System.out.println("T2:拿茶叶");
        TimeUnit.SECONDS.sleep(1);
        return "龙井";
    }
}
